import re

# 基础功能：字符数、单词数、句子数统计
def count_chars(content):
    """统计所有字符（包括空格、换行、标点）"""
    return len(content)

def count_words(content):
    """统计英文单词（连续字母序列）"""
    return len(re.findall(r'[a-zA-Z]+', content))

def count_sentences(content):
    """统计句子（以.?!结尾的视为句子）"""
    sentences = [s.strip() for s in re.split(r'[.!?]', content) if s.strip()]
    return len(sentences)

# 主程序：读取example.txt并统计
if __name__ == "__main__":
    # 读取外部文件example.txt
    try:
        with open("example.txt", "r", encoding="utf-8") as f:
            content = f.read()  # 读取文件内容
        
        # 计算统计结果
        chars = count_chars(content)
        words = count_words(content)
        sentences = count_sentences(content)

        # 输出结果
        print(f"===== example.txt 统计结果 =====")
        print(f"字符数：{chars}")
        print(f"单词数：{words}")
        print(f"句子数：{sentences}")
        print("===============================")
    
    except FileNotFoundError:
        print("错误：未找到 example.txt 文件，请确保该文件与 wc.py 在同一文件夹")